from collections import OrderedDict
from typing import List

from app.models.po.hanfan.hanfan_new_predict_po import HanfanNewPredictPO
from app.services.chart.Line import Line
from app.services.dal import hanfan_dao_service
from app.utils import date_util


class HanfanData(Line):

    def generate(self, start, end):
        start, end = date_util.parse_start_end_time(start, end, hour_interval=30 * 24)

        records: List[HanfanNewPredictPO] = hanfan_dao_service.get_new_hanfan_results(start, end)

        return dict(columns=
                    [{"title": "时间", "dataIndex": "DATE_TIME", "key": "DATE_TIME"},
                     {"title": "钒收得率", "dataIndex": "CG_LT_GL_GL04_Vshoude", "key": "CG_LT_GL_GL04_Vshoude"},
                     {"title": "铁水钒", "dataIndex": "CG_LT_GL_GL04_Tie_V", "key": "CG_LT_GL_GL04_Tie_V"},
                     {"title": "炉渣镁铝比", "dataIndex": "CG_LT_GL_GL04_Zha_Mg_Al", "key": "CG_LT_GL_GL04_Zha_Mg_Al"},
                     {"title": "炉渣碱度", "dataIndex": "CG_LT_GL_GL04_Zha_R", "key": "CG_LT_GL_GL04_Zha_R"},
                     {"title": "炉渣二氧化钛", "dataIndex": "CG_LT_GL_GL04_Zha_TiO2", "key": "CG_LT_GL_GL04_Zha_TiO2"},
                     {"title": "铁水温度", "dataIndex": "CG_LT_GL_GL04_Ftswd", "key": "CG_LT_GL_GL04_Ftswd"},
                     {"title": "冷风流量", "dataIndex": "CG_LT_GL_GL04_LFLL", "key": "CG_LT_GL_GL04_LFLL"},
                     {"title": "富氧流量", "dataIndex": "CG_LT_GL_GL04_FYLL", "key": "CG_LT_GL_GL04_FYLL"},
                     {"title": "鼓风动能", "dataIndex": "CG_LT_GL_GL04_GFDNKG", "key": "CG_LT_GL_GL04_GFDNKG"},
                     {"title": "富氧率", "dataIndex": "CG_LT_GL_GL04_FYL02", "key": "CG_LT_GL_GL04_FYL02"},
                     {"title": "风速实际", "dataIndex": "CG_LT_GL_GL04_FSSJ", "key": "CG_LT_GL_GL04_FSSJ"},
                     {"title": "阀座温度", "dataIndex": "CG_LT_GL_GL04_FZWD", "key": "CG_LT_GL_GL04_FZWD"},
                     {"title": "炉顶压力", "dataIndex": "CG_LT_GL_GL04_LDYL", "key": "CG_LT_GL_GL04_LDYL"},
                     {"title": "煤气利用率", "dataIndex": "CG_LT_GL_GL04_MQLYL", "key": "CG_LT_GL_GL04_MQLYL"},
                     ]
                    , data=records)

    def download(self, start, end):
        import pandas as pd
        results = []
        keys = ["DATE_TIME",
                "CG_LT_GL_GL04_Vshoude", "CG_LT_GL_GL04_Tie_V", "CG_LT_GL_GL04_Zha_Mg_Al",
                "CG_LT_GL_GL04_Zha_R", "CG_LT_GL_GL04_Zha_TiO2", "CG_LT_GL_GL04_Ftswd",
                "CG_LT_GL_GL04_LFLL", "CG_LT_GL_GL04_FYLL", "CG_LT_GL_GL04_GFDNKG",
                "CG_LT_GL_GL04_FYL02", "CG_LT_GL_GL04_FSSJ","CG_LT_GL_GL04_FZWD",
                "CG_LT_GL_GL04_LDYL", "CG_LT_GL_GL04_MQLYL",
                ]
        columns = ["时间",
                   "钒收得率", "铁水钒", "炉渣镁铝比",
                   "炉渣碱度", "炉渣二氧化钛", "铁水温度",
                   "冷风流量", "富氧流量", "鼓风动能",
                   "富氧率", "风速实际", "阀座温度",
                   "炉顶压力", "煤气利用率"]
        records: List[HanfanNewPredictPO] = hanfan_dao_service.get_new_hanfan_results(start, end)
        for record in records:
            ordered = OrderedDict()
            for index, key in enumerate(keys):
                column = columns[index]
                ordered[column] = record.__dict__.get(key,None)
            results.append(ordered)

        df = pd.DataFrame(data=results)

        return df
